package com.yxhl.zoume.core.coupon.adapter;

import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.yxhl.zoume.CurrentCouponItemDataBinding;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.data.http.model.coupon.CouponInfo;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.List;

/**
 * Created by vhawk on 2016/11/3.
 */
public class CurrentCouponAdapter extends BaseAdapter<CurrentCouponAdapter.CurrentCouponViewHolder> {

    List<CouponInfo> currentCoupons;

    public CurrentCouponAdapter setCurrentCoupons(List<CouponInfo> currentCoupons) {
        this.currentCoupons = currentCoupons;
        notifyDataSetChanged();
        return this;
    }

    @Override
    public CurrentCouponViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CurrentCouponItemDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_coupon_current, parent, false);
        CurrentCouponViewHolder holder = new CurrentCouponViewHolder(dataBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(CurrentCouponViewHolder holder, int position) {
        holder.bindTo(currentCoupons.get(position));
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isEmpty(currentCoupons)){
            return 0;
        }else {
            return currentCoupons.size();
        }
    }

    public class CurrentCouponViewHolder extends BaseAdapter.BaseViewHolder {
        CurrentCouponItemDataBinding dataBinding;
        public CurrentCouponViewHolder(CurrentCouponItemDataBinding dataBinding) {
            super(dataBinding.getRoot());
            this.dataBinding = dataBinding;
        }

        public void bindTo(CouponInfo couponInfo) {
            dataBinding.setCurrentCouponInfo(couponInfo);
        }
    }
}
